package com.qf.service;

import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.core.metadata.IPage;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.qf.mapper.DtsCategoryMapper;
import com.qf.pojo.DtsCategory;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.util.StringUtils;

import java.util.Date;
import java.util.List;

/**
 * @author fy
 * @version V1.0
 * @Project workspace
 * @Package com.qf.service
 * @Description:
 * @Date 2022/6/21 18:58
 */
@Service
public class CategoryServiceImpl implements CategoryService {
    @Autowired
    private DtsCategoryMapper categoryMapper;

    /**
     * 返回一级类目
     * @return
     */
    @Override
    public List<DtsCategory> categoryList1() {
        QueryWrapper<DtsCategory> queryWrapper = new QueryWrapper<>();
        queryWrapper.eq("deleted", false);
        queryWrapper.eq("pid", 0);
        List<DtsCategory> list = categoryMapper.selectList(queryWrapper);
        return list;
    }

    @Override
    public List<DtsCategory> categoryList2(Integer pid) {
        QueryWrapper<DtsCategory> queryWrapper = new QueryWrapper<>();
        queryWrapper.eq("deleted", false);
        queryWrapper.eq("pid", pid);
        List<DtsCategory> list = categoryMapper.selectList(queryWrapper);
        return list;
    }

    @Override
    public IPage<DtsCategory> queryCategoryPage(Integer id, String name, Integer page, Integer limit, String sort, String order) {
        IPage<DtsCategory> iPage = new Page<>(page,limit);
        //2.封装查询对象
        QueryWrapper<DtsCategory> queryWrapper = new QueryWrapper<>();
        queryWrapper.eq("deleted", false);
        //id不为空，校验
        if (!StringUtils.isEmpty(id)){
            queryWrapper.eq("id", id);
        }
        //name不为空，校验
        if (!StringUtils.isEmpty(name)){
            queryWrapper.like("name", name);
        }
        //排序
        if (!StringUtils.isEmpty(sort)&&!StringUtils.isEmpty(order)){
            //排序方式是否为升序
            if (order.equalsIgnoreCase("asc")){
                queryWrapper.orderByAsc(sort);
            }else {
                //降序
                queryWrapper.orderByDesc(sort);
            }

        }

        IPage<DtsCategory> resultPage = categoryMapper.selectPage(iPage, queryWrapper);

        return resultPage;
    }

    @Override
    public DtsCategory add(DtsCategory category) {
        category.setAddTime(new Date());
        category.setUpdateTime(new Date());
        categoryMapper.insert(category);
        return category;
    }

    @Override
    public int update(DtsCategory category) {
        category.setUpdateTime(new Date());
        int count = categoryMapper.updateById(category);
        return count;
    }

    @Override
    public void delete(Integer id) {
        QueryWrapper<DtsCategory> queryWrapper = new QueryWrapper<>();
        queryWrapper.eq("id", id);
        categoryMapper.delete(queryWrapper);
    }


}
